package cn.changeforyou.web.dao.mybatis.ext.metaInfo.parser;

import cn.changeforyou.base.exception.ExceptionFactory;
import cn.changeforyou.utils.string.StringUtils;
import cn.changeforyou.web.dao.mybatis.ext.exception.DaoExceptionEnum;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.ConditionCombineFieldMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.ConditionFieldMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.ConditionMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.condition.*;
import cn.changeforyou.web.utils.NameUtils;
import cn.hutool.core.util.ReflectUtil;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * ConditionMetaInfo解析器
 *
 * @author zhyu
 * @version 1.0
 * @date 2022/2/10 16:06
 * @see ConditionMetaInfo
 */
public class ConditionMetaInfoParser {

    /**
     * 解析条件类
     *
     * @param conditionClass
     * @return
     */
    public static ConditionMetaInfo parse(Class conditionClass) {
        ConditionMetaInfo conditionMetaInfo = new ConditionMetaInfo();
        conditionMetaInfo.setConditionClass(conditionClass);

        parseConditionFieldMetaInfo(conditionClass, conditionMetaInfo);
        parseConditionCombineFieldMetaInfo(conditionClass, conditionMetaInfo);

        return conditionMetaInfo;
    }

    /**
     * 解析联合字段元信息
     *
     * @param conditionClass
     * @param conditionMetaInfo
     */
    private static void parseConditionCombineFieldMetaInfo(Class conditionClass, ConditionMetaInfo conditionMetaInfo) {
        List<ConditionCombineFieldMetaInfo> list = new ArrayList<>(16);
        conditionMetaInfo.setConditionCombineFieldMetaInfoList(list);

        dealBracketWrapper(conditionClass, list);
        dealBetween(conditionClass, list);
    }

    /**
     * 处理between注解
     *
     * @param conditionClass
     * @param list
     */
    private static void dealBetween(Class conditionClass, List<ConditionCombineFieldMetaInfo> list) {
        Betweens betweens = AnnotationUtils.getAnnotation(conditionClass, Betweens.class);
        Between between = AnnotationUtils.getAnnotation(conditionClass, Between.class);
        if (null != betweens && null != between) {
            throw ExceptionFactory.jsonException(DaoExceptionEnum.ANNOTATION_COMBINE_ERROR);
        } else if (null != between) {
            parseBetween2ConditionCombineFieldMetaInfo(between, list);
        } else if (null != betweens) {
            Between[] betweenArray = betweens.value();
            if (betweenArray.length == 0) {
                throw ExceptionFactory.jsonException(DaoExceptionEnum.ANNOTATIONS_VALUE_IS_EMPTY);
            }
            for (Between item : betweenArray) {
                parseBetween2ConditionCombineFieldMetaInfo(item, list);
            }
        }
    }

    /**
     * 处理括号注解
     *
     * @param conditionClass
     * @param list
     */
    private static void dealBracketWrapper(Class conditionClass, List<ConditionCombineFieldMetaInfo> list) {
        BracketWrappers bracketWrappers = AnnotationUtils.getAnnotation(conditionClass, BracketWrappers.class);
        BracketWrapper bracketWrapper = AnnotationUtils.getAnnotation(conditionClass, BracketWrapper.class);
        if (null != bracketWrappers && null != bracketWrapper) {
            throw ExceptionFactory.jsonException(DaoExceptionEnum.ANNOTATION_COMBINE_ERROR);
        } else if (null != bracketWrapper) {
            parseBracket2ConditionCombineFieldMetaInfo(bracketWrapper, list);
        } else if (null != bracketWrappers) {
            BracketWrapper[] bracketWrapperArray = bracketWrappers.value();
            if (bracketWrapperArray.length == 0) {
                throw ExceptionFactory.jsonException(DaoExceptionEnum.ANNOTATIONS_VALUE_IS_EMPTY);
            }
            for (BracketWrapper wrapper : bracketWrapperArray) {
                parseBracket2ConditionCombineFieldMetaInfo(wrapper, list);
            }
        }
    }

    /**
     * 解析括号到条件联合字段元信息对象
     *
     * @param between
     * @param list
     */
    private static void parseBetween2ConditionCombineFieldMetaInfo(Between between,
                                                                   List<ConditionCombineFieldMetaInfo> list) {
        ConditionCombineFieldMetaInfo conditionCombineFieldMetaInfo = new ConditionCombineFieldMetaInfo();
        list.add(conditionCombineFieldMetaInfo);

        conditionCombineFieldMetaInfo.setType(CombineAnnotationEnum.between);
        conditionCombineFieldMetaInfo.setNotBetween(between.notBetween());
        conditionCombineFieldMetaInfo.setLeftBetween(between.leftBetween());
        conditionCombineFieldMetaInfo.setLeftAnd(between.leftAnd());
        conditionCombineFieldMetaInfo.setRightAnd(between.rightAnd());
        conditionCombineFieldMetaInfo.setKey(between.key());
        conditionCombineFieldMetaInfo.setParentKey(between.parentKey());
    }

    /**
     * 解析括号到条件联合字段元信息对象
     *
     * @param bracketWrapper
     * @param list
     */
    private static void parseBracket2ConditionCombineFieldMetaInfo(BracketWrapper bracketWrapper, List<ConditionCombineFieldMetaInfo> list) {
        ConditionCombineFieldMetaInfo conditionCombineFieldMetaInfo = new ConditionCombineFieldMetaInfo();
        list.add(conditionCombineFieldMetaInfo);

        conditionCombineFieldMetaInfo.setType(CombineAnnotationEnum.bracketWrapper);
        conditionCombineFieldMetaInfo.setKey(bracketWrapper.key());
        conditionCombineFieldMetaInfo.setParentKey(bracketWrapper.parentKey());
        conditionCombineFieldMetaInfo.setOr(bracketWrapper.or());
        conditionCombineFieldMetaInfo.setConditionKeys(bracketWrapper.conditionKeys());
    }

    /**
     * 解析字段元信息
     *
     * @param conditionClass
     * @param conditionMetaInfo
     */
    private static void parseConditionFieldMetaInfo(Class conditionClass, ConditionMetaInfo conditionMetaInfo) {
        List<ConditionFieldMetaInfo> conditionFieldMetaInfoList = new ArrayList<>();
        conditionMetaInfo.setConditionFieldMetaInfoList(conditionFieldMetaInfoList);
        for (Field field : ReflectUtil.getFields(conditionClass)) {
            //过滤掉静态变量
            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            }
            Conditions conditions = field.getAnnotation(Conditions.class);
            Condition condition = field.getAnnotation(Condition.class);
            if (null != condition && null != conditions) {
                throw ExceptionFactory.jsonException(DaoExceptionEnum.ANNOTATION_COMBINE_ERROR);
            }
            if (null != conditions) {
                Condition[] conditionArray = conditions.value();
                if (conditionArray.length == 0) {
                    throw ExceptionFactory.jsonException(DaoExceptionEnum.ANNOTATIONS_VALUE_IS_EMPTY);
                }
                for (Condition item : conditionArray) {
                    parseConditionAnnotation(conditionFieldMetaInfoList, field, item);
                }
            } else if (null != condition) {
                parseConditionAnnotation(conditionFieldMetaInfoList, field, condition);
            } else if (null == condition && null == conditions) {
                parseEmpty(conditionFieldMetaInfoList, field);
            }
        }
    }

    /**
     * 解析Condition注解
     *
     * @param conditionFieldMetaInfoList
     * @param field
     * @param annotation
     */
    private static void parseConditionAnnotation(List<ConditionFieldMetaInfo> conditionFieldMetaInfoList, Field field,
                                                 Condition annotation) {
        ConditionFieldMetaInfo con = new ConditionFieldMetaInfo();
        conditionFieldMetaInfoList.add(con);
        con.setFieldClass(field.getType());
        con.setFieldName(field.getName());

        String key = annotation.key();
        if (StringUtils.isBlank(key)) {
            key = "#" + field.getName();
        }

        con.setType(annotation.type());
        con.setColumnName(annotation.columnName());
        con.setOr(annotation.or());
        con.setPosition(annotation.position());
        con.setJdbcType(annotation.jdbcType());
        con.setTypeHandler(annotation.typeHandler());
        con.setAppendOrColumnIsNull(annotation.appendOrColumnIsNull());
        con.setKey(key);

        if (StringUtils.isBlank(con.getColumnName())) {
            con.setColumnName(NameUtils.getUnderLineName(field.getName()));
        }
    }

    /**
     * 解析没有注解的字段
     * @param conditionFieldMetaInfoList
     * @param field
     */
    private static void parseEmpty(List<ConditionFieldMetaInfo> conditionFieldMetaInfoList, Field field) {
        ConditionFieldMetaInfo con = new ConditionFieldMetaInfo();
        conditionFieldMetaInfoList.add(con);
        con.setFieldClass(field.getType());
        con.setFieldName(field.getName());

        String key = "#" + field.getName();

        con.setType(ConditionType.empty);
        con.setKey(key);
    }



}
